import javax.swing.text.DefaultEditorKit.CutAction;

public class HashBuck<K,V> {
    public static class Node<K,V>{
        public  K key;
        public  V val;
        public Node<K, V> next;
        public Node(K key,V val){
            this.key=key;
            this.val=val;
        }
    }
    public Node<K, V>[] array=new Node[10];
    public int usedSize;
    //	public Node<K, V>[] array=(Node<K,V>[])new Node[10];
    //插入
    public void put(K key, V val) {
        int hascode=key.hashCode();
        int index=hascode%array.length;
        Node cur=array[index];
        while(cur!=null) {
            if(cur.key.equals(key)) {
                cur.val=val;
                return;
            }
            cur=cur.next;
        }
        //没找到 要进行尾插
        Node<K, V> node=new Node<>(key, val);
//		node.next=array[index];
//		array[index]=node;
        cur=array[index];
        if(cur==null) {
            array[index]=node;

        }else {
            while (cur.next!=null) {
                cur=cur.next;
            }
            cur.next=node;
        }
        usedSize++;
        if(getPress()>=flag) {
            reSize();
        }
    }
    public static final float flag=0.75f;
    private  float getPress() {
        return usedSize*1.0f%array.length;

    }
    private void reSize() {
        Node<K, V>[] newArray=new Node[2*array.length];
        for(int i=0;i<array.length;i++) {
            Node<K, V> curNode=array[i];
            while (curNode!=null) {
                Node<K, V> curNextNode=curNode.next;
                int hashcode=curNode.key.hashCode();
                int index=hashcode%newArray.length;
                Node<K, V> cur=newArray[index];
                if(cur==null) newArray[index]=curNode;
                else {
                    while (cur.next!=null) {
                        cur=cur.next;
                    }
                    cur.next=curNode;
                }
                curNode=curNextNode;
            }
        }
        array=newArray;
    }
    //得到val值
    public V get(K key) {
        int index=key.hashCode()%array.length;
        Node<K, V> cur =array[index];
        while(cur!=null) {
            if(cur.key.equals(key)) {
                return cur.val;
            }
            cur=cur.next;
        }
        return null;
    }

}
